<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CreateUserRequest;
use App\Model\Device;
use App\Model\Group;
use App\Model\Site;
use App\Model\User;
use App\Model\UserManageSite;
use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function getUserSite(Request $request)
    {
        $user_id = $request->input('user_id');
        $user = User::find($user_id);
        $site_total = Site::getSitesTotalByUserId($user_id);
        return view('admin.site.user-site', [
            'user' => $user,
            'user_id' => $user_id,
            'site_total' => $site_total
        ]);
    }

    public function postUserSite(Request $request)
    {
        $user_id = $request->input('user_id');
        $sites = Site::getSitesByUserId($user_id);
        return $this->json_success($sites);
    }

    public function getCreateUserSite(Request $request)
    {
        $user_id = $request->input('user_id');
        $user = User::find($user_id);
        return view('admin.site.create-user-site', [
            'user' => $user
        ]);
    }

    public function postCreateUserSite(Request $request)
    {
        $params = $request->all();
        $site = Site::create($params);
        if (!$site) {
            return $this->json_sys_error();
        }
        //TODO 添加管理关系失败如何提示
        UserManageSite::addOwner($site->user_id, $site->id);
        return $this->json_success($site);
    }

    public function getEditUserSite(Request $request)
    {
        $user_id = $request->input('user_id');
        $id = $request->input('id');
        $user = User::find($user_id);
        $site = Site::find($id);
        return view('admin.site.edit-user-site', [
            'user' => $user,
            'site' => $site
        ]);
    }

    public function postEditUserSite(Request $request)
    {
        $user_id = $request->input('user_id');
        $id = $request->input('id');
        $params = $request->all();
        $user = User::find($user_id);
        $site = Site::find($id);
        if (!$site) {
            return $this->json_error('电站不存在！', 1001);
        }
        $site = $site->update($params);
        return $this->json_success($site);

    }

    public function postDeleteUserSite(Request $request)
    {
        $id = $request->input('id');
        $user_id = $request->input('user_id');
        $result = Site::deleteUserSite($user_id, $id);

        if ($result) {
            return $this->json_success();
        } else {
            return $this->json_sys_error();
        }

    }

    public function getUserSiteDetail(Request $request)
    {
        $id = $request->input('id');
        $user_id = $request->input('user_id');
        $user = User::find($user_id);
        $site = Site::find($id);

        $groups = Group::getGroupsBySiteId($id);
        foreach ($groups as $g) {
            $g->devices_count = Device::getDevicesCountByGroupId($g->id);
        }

        return view('admin.site.user-site-detail', [
            'groups' => $groups,
            'user' => $user,
            'site' => $site
        ]);
    }

}
